package com.bishe.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.entity.Club;
import com.bishe.entity.Notice;
import com.bishe.entity.User;
import com.bishe.service.ActivityService;
import com.bishe.entity.Activity;
import com.bishe.mapper.ActivityMapper;
import com.bishe.service.ClubService;
import com.bishe.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @description 针对表【activity】的数据库操作Service实现
* @createDate 2024-03-07 14:46:10
*/
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
    implements ActivityService {

    @Resource
    private UserService userService;

    @Resource
    private ClubService clubService;

    @Override
    public List<Activity> selectById(String id) {
        return list(Wrappers.lambdaQuery(Activity.class).eq(Activity::getUid, id).orderByDesc(Activity::getActivityTime));
    }

    @Override
    public List<Activity> clubNotice(String id) {
        User user = userService.getById(id);
        String clubId = user.getClubId();
        if (StrUtil.isEmpty(clubId)){
            return Collections.emptyList();
        }
        List<String> clubIds = Arrays.asList(clubId.split(","));
        List<Activity> noticeList = list(Wrappers.lambdaQuery(Activity.class).in(Activity::getUid, clubIds));

        Map<String, String> clubMap = clubService.list().stream().collect(Collectors.toMap(Club::getId, Club::getClubName));

        noticeList.forEach(s->{
            String name = clubMap.get(s.getUid());
            s.setClubName(name);
        });
        return noticeList;
    }
}




